<?php

define("USER_GUEST", 0);
define("USER_ADMIN", 1);
define("SESSION_TIME", 7200);
define("DEFAULT_PASSWORD", "123456");


/**
 * User class. Since OWL is single-user, this class is simple
 *
 * @package OWL
 * @subpackage Core
 * @author KeiMOon
 * @since 1.0
 */
class Owl_User {
    /**
     * Singleton instance
     *
     * @var User
     */
    private static $_instance;
    
    private $user_rank;

    private $user_lang;

    /**
     * Constructor, cannot be accessed.
     */
    private function  __construct() {
        $this->user_rank = USER_GUEST;
        $this->user_lang = false;
    }

    /**
     * Clone method, cannot be accessed.
     */
    private function __clone() {

    }

    /**
     * Object initialization method. Use this method instead of new operator.
     *
     * @return Owl_User singleton instance
     */
    static function get_instance() {
        if( self::$_instance === NULL ) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * Set up user
     *
     * @return bool
     */
    function setup_user() {
        //Check the $_SESSION first
        if (!isset($_SESSION["user"]) || time() - $_SESSION["user"]["time"] > SESSION_TIME) {
            
            //Authenticate by cookie
            if (@Owl_Cookie::get_hash_cookie_key("login") != "owl_login") {
                //Fail
                $this->create_guess_session();
            }
            else {
                list ($password, $salt) = $this->get_current_login_credentials();
                if (@Owl_Cookie::authenticate_hash_cookie("login", $password, $salt) == false) {
                    //Also fail
                    $this->create_guess_session();
                    
                }
                else {
                    $this->create_admin_session();
                }
            }
        }
        else {
            $this->user_rank = $_SESSION["user"]["rank"];
            $this->user_lang = $_SESSION["user"]["lang"];
        }
    }

    /**
     * User is logged in or not
     *
     * @return bool 
     */
    function is_logged_in() {
        return ($this->user_rank == USER_ADMIN);
    }

    /**
     * Get user session language
     *
     * @return string|bool
     */
    function get_user_lang() {
        return $this->user_lang;
    }

    /**
     * Check an attempt to log in
     *
     * @param string $password
     * @param string|bool $lang
     * @param bool $cookie
     * @return bool 
     */
    function log_in_user($password, $lang, $cookie) {
        //Check the password first
        list ($hashed_password, $salt) = $this->get_current_login_credentials();
        if (empty($hashed_password) && empty($password)) {
            //Create an admin session with language
            $this->create_admin_session($lang);

            return true;
        }
        if (Owl_System_Helper::hash(trim($password), trim($salt)) != trim($hashed_password)) {
            //Fail
            return false;
        }
        else {
            //Create an admin session with language
            $this->create_admin_session($lang);

            //If you want cookies then we shall give you one
            if ($cookie) {
                $this->set_login_cookie($hashed_password, $salt);
            }

            return true;
        }
    }

    /**
     * Log out an user
     */
    function log_out_user() {
        $this->create_guess_session();
        @Owl_Cookie::delete_hashed_cookie('login');
    }

    /**
     * Create guest session, if authentication fail
     */
    private function create_guess_session() {
        $_SESSION["user"]["rank"] = $this->user_rank = USER_GUEST;
        $_SESSION["user"]["lang"] = $this->user_lang = false;
        $_SESSION["user"]["time"] = time();
    }

    private function create_admin_session($lang = false) {
        $_SESSION["user"]["rank"] = $this->user_rank = USER_ADMIN;
        $_SESSION["user"]["lang"] = $this->user_lang = $lang;
        $_SESSION["user"]["time"] = time();
    }

    /**
     * Get user password and salt
     *
     * @return Array
     */
    private function get_current_login_credentials() {
        $owl_personal = new Owl_Personal();
        $personal_info = $owl_personal->get_personal();
        return array(trim($personal_info["password"]), trim($personal_info["salt"]));
    }

    /**
     * Create login cookie
     *
     * @param string $password
     * @param string $salt
     */
    private function set_login_cookie($password, $salt) {
        $name = "login";
        $key = "owl_login";
        return @Owl_Cookie::set_hash_cookie($name, $key, $password, $salt);
    }

    
}